


*************************************************************
************ Non-Bank Loans
*************************************************************

//Psedo-Data Needed 
use "../Intermediate/bdc_non_bank_loans", clear
use "../Intermediate/prequin_non_bank_loans", clear
use "../Intermediate/pitchbook_non_bank_loans", clear


//These three files go into this. So easier just to simulate 
use "../Intermediate/non_bank_loans_bank_ids", clear
set seed 12345

bsample
ds *debt* *lending*, has(type numeric)  
local to_random `r(varlist)'

foreach v of local to_random {
    quietly summarize `v', meanonly
    // draw Uniform(min, max) for each obs
    replace `v' = r(min) + runiform() * (r(max) - r(min))
}
save "../Intermediate/psedo/non_bank_loans_bank_ids.dta", replace

//Aggregate Version
use "../Intermediate/aggegate_non_bank_trans", clear
set seed 12345
foreach v of varlist year amount_trans_lender_usdm debt_quarter {
    quietly summarize `v', meanonly
    replace `v' = runiformint(r(min), r(max))
}
save "../Intermediate/psedo/aggegate_non_bank_trans.dta", replace

*************************************************************
************ Transaction Level Data Loans
*************************************************************

//IJ Global
use ../Intermediate/IJGlobal/transaction_level_data, clear
set seed 12345


bsample

ds, has(type numeric)
local numvars `r(varlist)'
local id "TransactionId"
local numvars : list numvars - id

foreach v of local numvars {
    quietly summarize `v', meanonly
    if r(N_nonmiss) > 0 {
        replace `v' = r(min) + runiform() * (r(max) - r(min))
    }
}
drop TransactionId
gen TransactionId = _n
unique TransactionId
save "../Intermediate/psedo/IJGlobal/transaction_level_data.dta", replace


//Dealscan
use "../Intermediate/Dealscan/tr_dealscan_newformat_tranchelevel", clear
set seed 12345
unique Borrower_Id

//Need to preserve LPC_Tranche_ID Tranche_Active_Date 
unique  LPC_Tranche_ID
*bsample
ds, has(type numeric)
local numvars `r(varlist)'
local id Borrower_Id LPC_Tranche_ID LPC_Deal_ID Tranche_Active_Date
local numvars : list numvars - id

foreach v of local numvars {
    quietly summarize `v', meanonly
    if r(N_nonmiss) > 0 {
        replace `v' = r(min) + runiform() * (r(max) - r(min))
    }
}

save "../Intermediate/psedo/Dealscan/tr_dealscan_newformat_tranchelevel.dta", replace


*************************************************************
************ dealscan tranche
*************************************************************

use ../Intermediate/Dealscan/tr_dealscan_newformat_tranchelender, clear
keep Lender_Share is_lead_bank_v1
set seed 12345
replace Lender_Share = runiform() * 100
replace is_lead_bank_v1 = runiform() < 0.5
gen n = _n
tempfile scramble
save `scramble'

use ../Intermediate/Dealscan/tr_dealscan_newformat_tranchelender, clear
drop Lender_Share is_lead_bank_v1
gen n = _n 
merge 1:1 n using `scramble', nogen
drop n
save "../Intermediate/psedo/Dealscan/tr_dealscan_newformat_tranchelender.dta", replace


*************************************************************
************ IJGlobal tranche
*************************************************************
use ../Intermediate/IJGlobal/debt_records_tranche_lender_level, clear
keep Lender_CompanyName TrancheRole LTAccreditedValuem Lender_ParentCompany
ds, has(type numeric)
local numvars `r(varlist)'
foreach v of local numvars {
    quietly summarize `v', meanonly
    if r(N_nonmiss) > 0 {
        replace `v' = r(min) + runiform() * (r(max) - r(min))
    }
}
bsample
gen n = _n
tempfile scramble
save `scramble'

use ../Intermediate/IJGlobal/debt_records_tranche_lender_level, clear
drop Lender_CompanyName TrancheRole LTAccreditedValuem Lender_ParentCompany
gen n = _n 
merge 1:1 n using `scramble', nogen
drop n
save "../Raw/psedo/IJGlobal/debt_records_tranche_lender_level.dta", replace


*************************************************************
************ Debt to tranche
*************************************************************
use ../Intermediate/IJGlobal/debt_records_tranchelevel, clear
drop TransactionId TrancheId
ds, has(type numeric)
local numvars `r(varlist)'
foreach v of local numvars {
    quietly summarize `v', meanonly
    if r(N_nonmiss) > 0 {
        replace `v' = r(min) + runiform() * (r(max) - r(min))
    }
}
bsample
gen n = _n
tempfile scramble
save `scramble'

use ../Intermediate/IJGlobal/debt_records_tranchelevel, clear
keep TransactionId TrancheId
gen n = _n
merge 1:1 n using `scramble', nogen
drop n
save "../Intermediate/psedo/IJGlobal/debt_records_tranchelevel", replace

*************************************************************
************ Transaction Level Data Bonds
*************************************************************
use "../Intermediate/clean_bonds_issued", clear
set seed 12345

drop companyid reference_id trancheid isin
//Scramble some numeric variables 
foreach v of varlist management_fee underwriting_fee allowance_fee principal_amt {
    quietly summarize `v', meanonly
    replace `v' = r(min) + runiform() * (r(max) - r(min))
}
bsample //Randomize
gen n = _n
tempfile scramble
save `scramble'

use "../Intermediate/clean_bonds_issued", clear
keep companyid reference_id trancheid isin
gen n = _n 
merge 1:1 n using `scramble', nogen
drop n


save "../Intermediate/psedo/clean_bonds_issued", replace


*************************************************************
************ Orbis Data
*************************************************************
use "../Intermediate/orbis_panel_borrower", clear
unique year borrower_id //Need to preserve
set seed 12345
drop borrower_id year
drop borrower_name BvDIDnumber Company_orbis
ds, has(type numeric)
local numvars `r(varlist)'
foreach v of local numvars {
    quietly summarize `v', meanonly
    if r(N_nonmiss) > 0 {
        replace `v' = r(min) + runiform() * (r(max) - r(min))
    }
}
bsample
gen n = _n
tempfile scramble
save `scramble'

use "../Intermediate/orbis_panel_borrower", clear
keep borrower_id year
gen n = _n
merge 1:1 n using `scramble', nogen
drop n
unique year borrower_id 
save  "/Users/jblume/Documents/GitHub/coal_finance/Accepted_Paper_Submission/Code_Replication_Package/Raw/psedo/orbis_panel_borrower.dta", replace








*************************************************************
************ EQUITY DATA
*************************************************************
use ../Intermediate/IJGlobal/equity_records_tranche_investor_level, clear

set seed 12345
foreach v of varlist SponsorEquityUSDm{
    quietly summarize `v', meanonly
    replace `v' = runiformint(r(min), r(max))
}
save "../Intermediate/psedo/IJGlobal/equity_records_tranche_investor_level.dta", replace




use ../Intermediate/clean_equity_issued, clear
keep principal_amt geo Exchange_WhereIss_WillBeL Issue_Type_Description Ticker_Symbol nation Issue_Date IPO_Flag__Y_N_ Type_of_Security
bsample
foreach v of varlist principal_amt {
    quietly summarize `v', meanonly
    replace `v' = runiformint(r(min), r(max))
}
gen n = _n
tempfile scramble
save `scramble'

use ../Intermediate/clean_equity_issued, clear
gen n = _n
drop principal_amt geo Exchange_WhereIss_WillBeL Issue_Type_Description Ticker_Symbol nation Issue_Date IPO_Flag__Y_N_ Type_of_Security
merge 1:1 n using `scramble', nogen
drop n
save "../Intermediate/psedo/clean_equity_issued", replace


*************************************************************
************ ESG DATA
*************************************************************
use ../Intermediate/esg_ratings/esg_banks_with_bans.dta, clear
drop BankName BankID

ds, has(type numeric)
local numvars `r(varlist)'
foreach v of local numvars {
    quietly summarize `v', meanonly
    if r(N_nonmiss) > 0 {
        replace `v' = r(min) + runiform() * (r(max) - r(min))
    }
}
bsample
gen n = _n
tempfile scramble
save `scramble'

use ../Intermediate/esg_ratings/esg_banks_with_bans.dta, clear
keep BankName BankID
gen n = _n
merge 1:1 n using `scramble', nogen
save "../Intermediate/psedo/esg_banks_with_bans", replace
